﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASoft.Base;
using System.Linq.Dynamic;

namespace ASoft.Web.Base
{
    public class WebRoomDataRepository : IRepository<WebRoomData>
    {
        public void Insert(WebRoomData entity)
        {
            if (entity == null)
            {
                return;
            }
            if (entity.Id < 1 || string.IsNullOrWhiteSpace(entity.CustomCode))
            {
                return;
            }
            datas[GetKey(entity)] = entity;
            entity.CustomCode = entity.CustomCode.ToLower();
        }

        public string GetKey(WebRoomData entity)
        {
            return string.Format("{0}--{1}", entity.CustomCode, entity.Id);
        }

        public List<WebRoomData> GetEntities(Dictionary<string, string> condition)
        {
            return GetEntities(condition, null, null);
        }

        List<WebRoomData> GetEntities(Dictionary<string, string> condition, string[] sortNames, string[] sortTypes)
        {
            var result = datas.Values.Where(p => p != null).AsQueryable();

            if (condition != null && condition.Count > 0)
            {
                var whereString = string.Join(" and ", condition.Where(p => !string.IsNullOrWhiteSpace(p.Value)).Select(p => string.Format("{0}=\"{1}\"", p.Key, p.Value)).ToArray());
                result = result.Where(whereString);
            }

            if (sortNames != null && sortNames.Length > 0)
            {
                result = result.OrderBy(string.Format("{0} {1}", sortNames[0], sortTypes[0]));
            }

            return result.ToList();
        }

        List<WebRoomData> GetEntities(List<WebRoomData> list, int pageIndex, int pageSize)
        {
            pageIndex = pageIndex < 1 ? 1 : pageIndex;
            pageSize = pageSize < 1 ? 20 : pageSize;
            return list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }

        public List<WebRoomData> GetEntities(Dictionary<string, string> condition, int pageIndex, int pageSize, string[] sortNames, string[] sortTypes)
        {
            return GetEntities(GetEntities(condition, sortNames, sortTypes), pageIndex, pageSize);
        }

        public PageResult<WebRoomData> GetPageResult(Dictionary<string, string> condition, int pageIndex, int pageSize, string[] sortNames, string[] sortTypes)
        {
            var list = GetEntities(condition, sortNames, sortTypes);
            return new PageResult<WebRoomData>(GetEntities(list, pageIndex, pageSize), pageIndex, pageSize, list.Count);
        }

        public string DBFileName { get; set; }

        #region Cache

        static Dictionary<string, WebRoomData> datas = new Dictionary<string, WebRoomData>();

        static Dictionary<string, WeatherStationData> wsDatas = new Dictionary<string, WeatherStationData>();

        #endregion


        public void Update(WebRoomData entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        #region WS
        public void Insert(WeatherStationData entity)
        {
            if (entity == null)
            {
                return;
            }
            if (string.IsNullOrWhiteSpace(entity.CustomCode))
            {
                return;
            }
            wsDatas[entity.CustomCode.ToLower()] = entity;
        }

        public WeatherStationData GetWSData(string customCode)
        {
            if (string.IsNullOrWhiteSpace(customCode))
            {
                return null;
            }
            if (!wsDatas.Keys.Contains(customCode.ToLower()))
            {
                return null;
            }
            return wsDatas[customCode.ToLower()];
        }
        #endregion
    }
}
